﻿@using System.Text.RegularExpressions;
@{
    var path = Server.MapPath("~/App_Data/");
    var db = Database.Open((string)App.Database);
    var commandText = @"SELECT COUNT(1) FROM Users";
    if((int)db.QueryValue(commandText) > 0){
        Response.SetStatus(HttpStatusCode.BadRequest);
    }
    Validation.RequireField("UserName");
    Validation.RequireField("Password", "Password cannot be empty");
    Validation.Add("Password2",
        Validator.Required("Put your password in here again"),
        Validator.EqualsTo("Password", "Doesn't match")
        );
    if (IsPost && Validation.IsValid()) {
        var ddl = Path.Combine(path, "scripts", "ddl.sql");
        var separators = new string[] { "GO" };
        var commands = File.ReadAllText(ddl).Split(separators, StringSplitOptions.RemoveEmptyEntries);
        foreach(var command in commands){
            if(!command.StartsWith("-") && Regex.IsMatch(command, @"[a-zA-Z]+")){
                db.Execute(command);
            }
        }
        var username = Request["UserName"];
        var password = Request["Password"];
        WebSecurity.CreateUserAndAccount(username, password);
        var data = Path.Combine(path, "scripts", "data.sql");
        commands = File.ReadAllText(data).Split(separators, StringSplitOptions.RemoveEmptyEntries);
        foreach(var command in commands){
            if(!command.StartsWith("-") && Regex.IsMatch(command, @"[a-zA-Z]+")){
                db.Execute(command);
            }
        }
        Roles.AddUsersToRoles(new[] { username }, new[] { "Administrator" });
        Response.Redirect("~/Admin");
    }
}
<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>Welcome to Web Pages CMS</title>
    <link href="~/Content/bootstrap.min.css" rel="stylesheet" media="screen">
    <script src="~/Scripts/jquery-1.9.1.min.js" type="text/javascript"></script>
    <script src="~/Scripts/jquery.validate.min.js" type="text/javascript"></script>
    <script src="~/Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>
    <script src="~/Scripts/bootstrap.min.js" type="text/javascript"></script>
    <script>
        $(function () {
            $('#modal').modal();
        });
    </script>
</head>
<body>
    <div id="modal" role="dialog" class="modal hide fade">
        <form method="post" class="modal-form">
            <div class="modal-header">
                <h3>Welcome to Web Pages CMS</h3>
            </div>
            <div class="modal-body">
                <p>The first step to using Web Pages CMS is to create a user with Administrator rights. 
                    Since this account can access all areas, you should ensure that the user name and password combination is not one that can easily be guessed.</p>
                @ControlGroup.TextBox("User Name: ", "UserName")
                @ControlGroup.Password("Password: ", "Password")
                @ControlGroup.Password("Confirm Password: ", "Password2")
            </div>
            <div class="modal-footer">
                @ControlGroup.Button(text: "Create User")
            </div>

        </form>
    </div>
</body>
</html>
